/* ************************************************************************************************

	Copyright (c) 2005-2006, IGR
	All Rights Reserved.
	Licensed under the Apache License version 2.0 or above.
	For more information on licensing, see:
        http://www.apache.org/licenses/


************************************************************************************************
 Objet type google CALENDAR
 VERSION:0.85
 DATE:16/03/2006
************************************************************************************************
 Init parameter :
  top:           	 Top position (in pixel)
  left:              Left position (in pixel)    
  objPar              object of parameter
 		dateMin		     each date before is disable
		dateMax		     each date after  is disable
        currentDate	     current date when display calendar
        bOpenOnLastDate  if true the calendar will open on the last date selected
        bUkFormat	     if true the date will be like 'MM/DD/YYYY' (DD/MM/YYYY if not)
************************************************************************************************/

rialto.widget.GoogleCalendar=function(objPar) {   

	//base constructor    
    objPar.type="googlecalendar";    
    this.base = rialto.widget.Calendar;  
    this.base(objPar);
    //Default value
}

rialto.widget.GoogleCalendar.prototype = new rialto.widget.Calendar;

	
rialto.widget.GoogleCalendar.prototype.loadSpecialDate=function(url,color){
		this.color=color?color:'#99CCFF';
		this.remote=new rialto.io.AjaxRequest({
			url:"/rialto/serverSideProxy?URL="+url,
			//url:url,
			withWaitWindow:true,
			callBackObjectOnSuccess:this,
			onSuccess: this.refreshSpecialDate
		})
		this.remote.load("");
	}
	
rialto.widget.GoogleCalendar.prototype.refreshSpecialDate=function(request){				
		try{
			var xmlDoc = request.responseXML;
			var arr=xmlDoc.getElementsByTagName("entry");		
			for(var i=0;i<arr.length;i++){		
				var entry=arr[i];
				//Title
		        var nodeTitle= entry.getElementsByTagName("title")[0];
		        var titleValue=nodeTitle.firstChild.nodeValue;		        
				//Try with gd:when tag
		        //Date
	   	        if(rialtoConfig.userAgentIsIE){
	   	        	var nodeDate= entry.getElementsByTagName("gd:when")[0];
	   	        }
	   	        else{
	  	        	var nodeDate= entry.getElementsByTagName("when")[0];
	   	        }
	   	        if(nodeDate){
		   	        var startTime=nodeDate.getAttribute("startTime");
		   	        var endTime=nodeDate.getAttribute("endTime");
					startTime=startTime.substr(0,4)+startTime.substr(5,2)+startTime.substr(8,2);				
					endTime=endTime.substr(0,4)+endTime.substr(5,2)+endTime.substr(8,2);							  	        
				}
				else{		
					//Try with summary tag					
					var nodeDate= entry.getElementsByTagName("summary")[0];
					var nodeDateValue=nodeDate.firstChild.nodeValue;
					//at this point we ahve a string like this: When: 2006-07-09 to 2006-07-10 Europe/Paris
					//kneed to extract the start time and the end time
					var arrD=nodeDateValue.split(" ");
					//the 2 element is always the starttime					
					var startTime=rialto.string.replace(arrD[1],"-","");				
					//the 2nd date if exist is after the "to"
					var j=2;
					while(arrD[j]!="to"){j+=1};
					var endTime=arrD[j+1];
					//if - in the value it is a date else it is an hour
					if(endTime.indexOf("-")!=-1){
						endTime=parseInt(rialto.string.replace(endTime,"-",""))-1+"";				
					}
					else{
						endTime=startTime;
					}
				}			
				var dstarTime=rialto.date.setDateFromYYYYMMDD(startTime);
				var dendTime=rialto.date.setDateFromYYYYMMDD(endTime);
				this.setSpecialDate([dstarTime,dendTime],this.color,titleValue);
			}	
			this.fillCalendar();
		}
		catch (error){
				alert(error);
		}
	}